//
//  SecondViewController.m
//  MaxMovie
//
//  Created by Maxdong on 14-12-27.
//  Copyright (c) 2014年 lanou3g.com 蓝欧科技. All rights reserved.
//

#import "SecondViewController.h"
#import  "ImageViewController.h"
@interface SecondViewController ()<UIScrollViewDelegate>

@end

@implementation SecondViewController

- (void)dealloc
{
    [_imageName release];
    [_imageArray release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.imageArray = [NSArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createScroll];
}
- (void)createScroll
{
    
    NSMutableArray *array = [NSMutableArray arrayWithArray:_imageArray];
    
    NSLog(@"发个方法%@", _imageArray);
    [array insertObject:[_imageArray lastObject] atIndex:0];
    [array addObject:[_imageArray objectAtIndex:0]];
    
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    [scroll setContentSize:CGSizeMake([array count] * scroll.frame.size.width, 0)];
    scroll.delegate = self;
    scroll.tag = 100;
    [scroll setPagingEnabled:YES];
    [self.view addSubview:scroll];
    [scroll release];
    
    for (int i = 0; i < [array count]; i++) {
        UIScrollView *little = [[UIScrollView alloc] initWithFrame:CGRectMake(i * scroll.frame.size.width, 0, scroll.frame.size.width, scroll.frame.size.height)];
        little.delegate = self;
        little.maximumZoomScale = 2.0f;
        little.minimumZoomScale = 0.5f;
        [scroll addSubview:little];
        [little release];
        
        UIImageView *aImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, little.frame.size.width, little.frame.size.height)];
        [aImage setImage:[UIImage imageNamed:[array objectAtIndex:i]]];
        [little addSubview:aImage];
        [aImage release];
    }
    
    for (int i = 1; i < [array count]; i++) {
        NSString *name = [array objectAtIndex:i];
        if ([name isEqualToString:self.imageName]) {
            [scroll setContentOffset:CGPointMake(i * scroll.frame.size.width, 0)];
            break;
        }
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    if (scrollView.tag == 100) {
        if (scrollView.contentOffset.x == 0) {
            [scrollView setContentOffset:CGPointMake([_imageArray count] * scrollView.frame.size.width, 0)];
        } else if (scrollView.contentOffset.x == ([_imageArray count]+ 1) * scrollView.frame.size.width) {
            [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0)];
        }
        
        NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
        if (self.currentIndex != index) {
            for (UIScrollView *aView in [scrollView subviews]) {
                // 判断当前对象是不是相对应的类型
                if ([aView isKindOfClass:[UIScrollView class]]) {
                    [aView setZoomScale:1.0f];
                }
            }
        }
        self.currentIndex = -1;
        
        
        
    }
    
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    
    if (scrollView.tag != 100) {
        UIScrollView *big = (UIScrollView *)[self.view viewWithTag:100];
        self.currentIndex = 0;
        for (UIScrollView *scroll in [big subviews]) {
            if (scroll == scrollView) {
                break;
            }
            _currentIndex++;
        }
        
        UIView *aView = [[scrollView subviews] objectAtIndex:0];
        return aView;
    }
    return nil;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
